﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace NASA.BeAMartian.Execution
{
    public abstract class ExecutionItem
    {
        /// <summary>
        /// Fired when an action completed regardless of whether an exception was thrown
        /// the action was canceled.
        /// </summary>
        public event EventHandler<ExecutionItemCompletedEventArgs> ActionCompleted;

        public ExecutionItem() { }

        /// <summary>
        /// Invokes the Execution Item
        /// </summary>
        public abstract void Invoke();

        /// <summary>
        /// Cancels the execution Item.
        /// </summary>
        public virtual void Cancel()
        {
            this.IsCanceled = true;
        }

        /// <summary>
        /// Returns the canceled status.
        /// </summary>
        public bool IsCanceled 
        {
            get { return this.Status == ExecutionStatusType.Canceled; } 
            protected set { this.Status = ExecutionStatusType.Canceled; }
        }

        // TODO: complete the use of Status.
        public ExecutionStatusType Status { get; internal set; }

        public bool ContinueOnError { get; internal set;  }

        public string Name { get; set; }

        protected void OnActionCompleted()
        {
            if (this.ActionCompleted != null)
                this.ActionCompleted(this, new ExecutionItemCompletedEventArgs());
        }

        protected void OnActionCompleted(Exception error, bool canceled)
        {
            if (this.ActionCompleted != null)
                this.ActionCompleted(this, new ExecutionItemCompletedEventArgs(error, canceled));
        }
    }
}
